package Collection;

import domain.Student;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

public class CollectionTest2 {
    public static void main(String[] args) {
        Collection<Student> c1=new ArrayList<>();

        c1.add(new Student("zhangsan",20));
        c1.add(new Student("lisi",22));
        c1.add(new Student("lose",23));
        Iterator<Student> it=c1.iterator();

//        while(true){
//            try {
//                Student stu1=it.next();
//                System.out.println(stu1);
//            } catch (NoSuchElementException e) {
//                break;
//            }
//        }
        while(it.hasNext()){
            Student stu1=it.next();
            System.out.println(stu1);
        }
//        增强for循环
        System.out.println("............");
        for (Student stu :c1){
            System.out.println(stu);
        }
        for (Student student : c1) {
            System.out.println(student);
        }
        System.out.println("............");
//foreach方法遍历集合
        c1.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        });
        System.out.println("............");
        c1.forEach(student -> {
            System.out.println(student);
        });
        System.out.println("+++++++");
        c1.forEach(student -> System.out.println(student));

        while(it.hasNext()){
            Student stu1=it.next();
            System.out.println(stu1.getName()+"----"+stu1.getAge());
        }
        for (Student stu2 : c1) {
            System.out.println(stu2);
        }
        c1.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        });
        c1.forEach(student -> {
            System.out.println(student);
        });
        Iterator<Student> it2=c1.iterator();
        while(it2.hasNext()){
            Student stu2=it2.next();
            System.out.println(stu2);
        }
        Iterator<Student> it3=c1.iterator();
        while(it3.hasNext()){
            Student stu3=it3.next();
            System.out.println(stu3.getName()+"____"+stu3.getAge());
        }
    }
}
